﻿using System;
//using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.AccessControl;

namespace Synchro
{
    public class Google
    {
        Credentials _credentials = null;
        CalendarService _googleService = null;
        List<CalendarEntry> _googleCalendars=new List<CalendarEntry>();
        bool _connected = false;
        string _calendarUri = null;

        public void insertEntry(EventEntry entry)
        {
            _googleService.Insert(new Uri(_calendarUri), entry);
        }

        public Google(Credentials credentials)
        {
            _credentials = credentials;
        }

        public string CalendarUri
        {
            get {return _calendarUri;}
            set {_calendarUri = value;}
        }

        public bool connect()
        {
            _googleService = new CalendarService("GoogleService");
            try
            {
                _googleService.setUserCredentials(_credentials.User, _credentials.Password);
                _connected = true;
                return true;
            } catch (AuthenticationException e)
            {
                return false;
            }
        }

        public bool isConnected()
        {
            return _connected;
        }

        public ArrayList updateCalendars()
        {
            _googleCalendars.Clear();
            ArrayList calendars = new ArrayList();
            bool success = connect();
            if (!success)
            {
                return null;
            }
            CalendarQuery query = new CalendarQuery("http://www.google.com/calendar/feeds/default/allcalendars/full");
            CalendarFeed resultFeed = (CalendarFeed)_googleService.Query(query);
            foreach (CalendarEntry entry in resultFeed.Entries)
            {
                calendars.Add(entry.Title.Text);
                _googleCalendars.Add(entry);
            }
            return calendars;
        }

        public void modifyUri(int index)
        {
            _calendarUri = _googleCalendars[index].EditUri.ToString().Replace("default/allcalendars/full/", "") + "/private/full";
        }

        public EventFeed getAllEntries(int index)
        {
            bool success = connect();
            // Create the query object:
            //EventQuery query = new EventQuery("http://www.google.com/calendar/feeds/" + _credentials.User + "@gmail.com/private/full");
            //EventQuery query = new EventQuery("http://www.google.com/calendar/feeds/cc37o6igg55lsmm368ka2tad20@group.calendar.google.com/private/full");
            //EventQuery query = new EventQuery(_googleCalendars[index].EditUri.ToString().Replace("default/allcalendars/full/","")+"/private/full");
            EventQuery query = new EventQuery(_calendarUri);

            // Tell the service to query:
            EventFeed calFeed = _googleService.Query(query);
            return calFeed;
        }
    }
}
